/*
 * @Author: 张升 1562821455@qq.com
 * @Date: 2024-08-07 11:44:32
 * @LastEditors: 张升 1562821455@qq.com
 * @LastEditTime: 2024-11-14 17:09:25
 * @FilePath: \HomeBigScreen\src\utils\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from 'axios'
import Cookies from 'js-cookie'
// import { getQueryString } from './method'

// let url = import.meta.env.VITE_API_URL
let url = 'http://devapi.isuke.com.cn'
let timeout = 20000

// 创建axios实例
const service = axios.create({
    baseURL: url,
    timeout: timeout // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
    config => {
        if (Cookies.get('token')) {
            config.headers['token'] = Cookies.get('token') // 让每个请求携带自定义token
        }
        if (config.contentType) config.headers['Content-Type'] = config.contentType
        return config
    },
    error => {
        console.error('request error', error)
        Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
        const code = response.status
        if (code < 200 || code > 300) {
            return Promise.reject('error')
        } else {
            return response.data
        }
    },
    error => {
        return Promise.reject(error)
    }
)



export default service
